//index.js
//获取应用实例 
var app = getApp()
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    token: '',
    accountId: '',
    codeId: '',
    passId: '',
    paddPassId: '',
    inviteId: '',
    inviteCode: '',
    code: '', //验证码
    iscode: null, //用于存放验证码接口里获取到的code
    codename: '获取验证码',
    temp: ''
  },
  onShow: function () {

  },
  onReady: function () {
    // wx.showLoading({
    //   title: '正在检查登录状态',
    // })
  },

  toHome: function () {
    wx.switchTab({
      url: '/pages/live/home',
    })
  },

  onLoad: function () {
    /**
     * 生命周期函数--监听页面加载
     */
    var that = this;
    //查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          console.log("用户授权了");
        } else {
          //用户没有授权
          console.log("用户没有授权");
        }
      }
    });
  },
  bindGetUserInfo: function (res) {
    if (res.detail.userInfo) {
      console.log(res)
      //用户按了允许授权按钮
      var that = this;
      // 获取到用户的信息了，打印到控制台上看下
      console.log("用户的信息如下：");
      console.log(res.detail.userInfo);
      app.globalData.userInfo = res.detail.userInfo
      //授权成功后,通过改变 isHide 的值，让实现页面显示出来，把授权页面隐藏起来
      that.setData({
        isHide: false
      });
      wx.login({
        success: function (res) {
          console.log(res)
          if (res.code) {
            var data;
            if (that.data.superUserId == '') {
              data = {
                jsCode: res.code,
              }
            } else {
              data = {
                jsCode: res.code,
              }
            }
            that.getOpenId(res.code);
          } else {
            console.log("授权失败");
          }
        },
      })
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          // 用户没有授权成功，不需要改变 isHide 的值
          if (res.confirm) {
            console.log('用户点击了“返回授权”');
          }
        }
      });
    }
  },

  accountInput: function (e) {
    var that = this;
    that.setData({
      accountId: e.detail.value
    })
  },
  passwordInput: function (e) {
    var that = this;
    that.setData({
      passId: e.detail.value
    })
  },
  tologin: function () {
    wx.navigateTo({
      url: 'phone-login',
    })
  },

  onReady: function () {

  },

  register: function () {
    wx.navigateTo({
      url: 'phone-login',
    })
  },
  //登录接口
  login: function () {
    wx.showLoading({
      title: '登录中...',
    })
    var that = this;
    console.log(that.data.inviteId)
    if (that.data.accountId == "") {
      wx.showToast({
        title: '请输入手机号', //提示文字
        duration: 2000, //显示时长
        icon: 'none',
        mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
        success: function () {}, //接口调用成功
        fail: function () {}, //接口调用失败的回调函数  
        complete: function () {} //接口调用结束的回调函数  
      })
      return;
    }
    if (that.data.passId == "") {
      wx.showToast({
        title: '请输入密码', //提示文字
        duration: 2000, //显示时长
        icon: 'none',
        mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
        success: function () {}, //接口调用成功
        fail: function () {}, //接口调用失败的回调函数  
        complete: function () {} //接口调用结束的回调函数  
      })
      return;
    }
    wx.request({
      url: app.globalData.api + '/index.php/wap/UserAccount/login',
      data: {
        account: that.data.accountId, //手机号码
        pwd: that.data.passId, //密码
        platform: 'miniwx'
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.code == 1) {
          app.globalData.token = res.data.data.token,
            app.globalData.uid = res.data.data.uid,
            app.globalData.group_id = res.data.data.group_id,

            wx.setStorage({ //缓存code
              key: "token",
              data: res.data.data.token
            })
          wx.switchTab({
            url: '/pages/shop/list',
          })

        } else {
          wx.showToast({
            title: '' + res.data.msg, //提示文字
            duration: 2000, //显示时长
            icon: 'none',
            mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
            success: function () {}, //接口调用成功
            fail: function () {}, //接口调用失败的回调函数  
            complete: function () {} //接口调用结束的回调函数  
          })
        }
      },
      fail: function () {

      },
      complete() {

      }
    });
  },

  getOpenId(code) {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=UserAccount&a=getWxOpenid',
      data: {
        code: code, //
        type:'2'
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        console.log(res.data.code)
        if (res.data.code == 0) {
          if (res.data.data.unionid) {
            app.globalData.openid = res.data.data.unionid
          } else {
            app.globalData.openid = res.data.data.openid
          }
          that.checkRegisterOauth(app.globalData.openid);
        } else {
          wx.showToast({
            title: '' + res.data.msg, //提示文字
            duration: 2000, //显示时长
            icon: 'none',
            mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
            success: function () {}, //接口调用成功
            fail: function () {}, //接口调用失败的回调函数  
            complete: function () {} //接口调用结束的回调函数  
          })
        }
      },
      fail: function () {

      },
      complete() {}
    });
  },

  //判断第三方社交平台账号是否注册
  checkRegisterOauth(openid) {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=UserAccount&a=checkRegisterOauth',
      data: {
        openid: openid,
        unionid: openid,
        type: 'wx',
        platform: 'miniwx'
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.code == 0) {
          //已经注册过，绑定了手机号码
          wx.setStorageSync('token', res.data.data.token); //将userIdEnc存入本地缓存
          wx.setStorageSync('uid', res.data.data.uid); //将userIdEnc存入本地缓存
          app.globalData.token = res.data.data.token
          app.globalData.uid = res.data.data.uid,
            wx.switchTab({
              url: '/pages/live/home',
            })
        } else {
          //需要绑定手机号
          wx.redirectTo({
            url: '/pages/wmm/bind',
          })
        }
      },
      fail: function () {

      },
      complete() {}
    });
  },
  getCode: function () {
    var a = this.data.accountId;
    var _this = this;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(this.data.accountId)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      console.log(this.data.accountId)
      wx.request({
        url: app.globalData.api + '/index.php/wap/Sms/sendUserRegister',
        data: {
          'phone': this.data.accountId, //手机号码
        },
        headers: {
          'platform': '' + app.globalData.platform,
          'version': '' + app.globalData.version,
        },
        method: 'POST',
        dataType: 'json',
        success(res) {
          console.log(res.data.data)
          _this.setData({
            iscode: res.data.data
          })
          var num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              _this.setData({
                codename: '重新发送',
                disabled: false
              })

            } else {
              _this.setData({
                codename: num + "s"
              })
            }
          }, 1000)
        }
      })

    }
  },
  //获取验证码
  getVerificationCode: function () {
    this.getCode();
    var _this = this
    _this.setData({
      disabled: true
    })
  },
})